<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		getEditorTitle,
		hasEditor,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this,
			};
		},
		data() {
			return buildFormData({
				checkUserCacheKey:'new-zl-inspectionReprot-checkUser-cache',
				acceptCheckInstCacheKey:'new-zl-inspectionReprot-acceptCheckInst-cache',
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			onSelectCheckDate(date){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				_that.om.request(
					'inspectionReport/getInitDataList',
					{
						checkUserId: formdata.checkUserId,
						checkLastDate: formdata.checkLastDate,
						checkDate: date,
						acceptCheckInstId: formdata.acceptCheckInstId
					}
				).then(res=>{
					if(res.succeed){
						_that.subEditorObj["title2"].workList = res.value
					}
				})
			},
			verifyTitle2(){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(!formdata.acceptCheckInstId){
					let title = _that.$refs.autoform.getEditorTitle("title1","acceptCheckInstId")
					_that.om.toast("请先选择"+title)
					return false
				}else{
					return true
				}
			},
			clearCheckUser(){
				let _that = this
				if(_that.subEditorObj["title2"]){
					_that.subEditorObj["title2"].workList = []
				}
			},
			clearAcceptCheckInst(){
				let _that = this
				if(_that.subEditorObj["title2"]){
					_that.subEditorObj["title2"].workList = []
				}
			},
			formSubmit(){
				let _that = this
				if(_that.edit){
					_that.om.formSave("inspectionReport/doUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("inspectionReport/doAdd",_that.formdata,_that)
				}
			},
			getWorkParam(){
				let _that = this
				return{
					checkUserId:_that.$refs.autoform.formdata.checkUserId,
					checkLastDate:_that.$refs.autoform.formdata.checkLastDate,
					checkDate:_that.$refs.autoform.formdata.checkDate,
					checkDateTitle:_that.$refs.autoform.getEditorTitle("title1",'checkDate'),
					instId:_that.$refs.autoform.formdata.acceptCheckInstId,
				}
			},
			getInitDataList(item){
				let _that = this
				if(item){
					return {
							url:'inspectionReport/getInitDataList',
							param:{
								checkUserId: item.userId,
								checkPosId: item.posId,
								checkDate: item.date
							}
						}
				}else{
					return {
							url:'inspectionReport/getInitDataList',
							param:{
								checkUserId: _that.userId,
								checkPosId: _that.salaryPosId,
								checkDate:moment().format('YYYY-MM-DD')
							}
						}
				}
				
			},
			initLoad(options){
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					_that.getInitDataList(),
					{
						url:'orgInstPosUserOfficial/findInstByUserId',
						param:{
							userid:_that.userId
						}
					},
					{
						url:'commonForm/hasMainPos',
						param:{
							busCode: options.code
						},
						method:'post'
					}
				]
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push(
						{
							url:'inspectionReport/getValue',
							param:{
								fileId:fileId
							}
						},
					)
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					let instIds = res[2].map(e=>e.id).join(',')
					changeEditor("title1", "checkUserId", {
						link: "../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({
								firstUserId: _that.userId,
								initEmptyFlag: false
							}),
							namefield:'userPosInstName',
							searchfield: "q",
							url:'temployee/getStaffCombogrid',
							cachename: _that.checkUserCacheKey,
							initload: true,
							placeholder: "请输入姓名"
						}
					}, null, mainData)
					changeEditor("title1","acceptCheckInstId",{
						"link":'../../public/search/search',
						"linkParam":{
							other:JSON.stringify(),
							url:'tinstitution/getComboTree',
							namefield:'text',
							initload:true,
							cachename:_that.acceptCheckInstCacheKey
						}
					},null,mainData)
					if(_that.subEditorObj["title2"]){
						let att = {
							getWorkParam:'getWorkParam'
						}
						let configData = _that.subConfigData["title2"]
						if(hasEditor("title2","userId",configData)){
							att.titleTemplate = "<%= userName || '无' %>"
						}
						if(hasEditor("title2","itemId",configData)){
							let title = getEditorTitle("title2","itemId",configData)
							att.subtitle1Template = title+":<%= itemName || '无'%>"
						}
						changeEditor("title2","dataStr",att,mainData)
					}
					if(_that.subEditorObj["title2"]){
						_that.subEditorObj["title2"].workList = res[1].value
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push('id')
						for(let index in res[4].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[4].value[index])
								_that.$set(_that.formdata,index,res[4].value[index])
							}
						}
						if(_that.subEditorObj["title2"]){
							let dataList = _that._.cloneDeep(res[4].value.dataList)
							_that.subEditorObj["title2"].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(dataList))
							_that.$set(_that.formdata,"dataStr",JSON.stringify(dataList))
						}
						_that.$nextTick(function(){
							_that.$refs.autoform.changeEditor("title2","checkUserId","type","labelbox")
							if(_that.$refs.autoform.formdata.zposId){
								_that.$refs.autoform.initSelectIndex("topinfo","zposId")
							}
						})
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}
						if(_that.$refs.autoform.hasEditor("title1","checkUserId") && !_that.$refs.autoform.formdata.checkUserId){
							_that.$refs.autoform.formdata.checkUserId = _that.userId
							_that.$refs.autoform.formdata.checkUserName = _that.userName
						}
						if(!_that.$refs.autoform.formdata.checkDate){
							_that.$refs.autoform.formdata.checkDate = moment().format('YYYY-MM-DD')
						}
						if(!_that.$refs.autoform.formdata.checkLastDate){
							_that.$refs.autoform.formdata.checkLastDate = moment().format('YYYY-MM-DD')
						}
					})
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onUnload(){
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for(let fieldId in _that.subEditorObj){
					let editor = _that.subEditorObj[fieldId]
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							for(let item of res.data){
								if(!item.fileList){
									item.fileList = []
								}
							}
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.checkUserCacheKey,
					success:(res)=>{
						if(res.data){
							let formdata = _that.$refs.autoform.formdata
							let d = res.data
							formdata.checkUserId = d.id
							formdata.checkUserName = d.username
							uni.removeStorage({
								key:_that.checkUserCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.acceptCheckInstCacheKey,
					success:(res)=>{
						if(res.data){
							let formdata = _that.$refs.autoform.formdata
							let d = res.data
							formdata.acceptCheckInstId = d.id
							formdata.acceptCheckInstName = d.text
							uni.removeStorage({
								key:_that.acceptCheckInstCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>
<style></style>